package com.lzh.msm.service.impl;

import com.lzh.msm.service.MsmService;
import com.lzh.msm.utils.TencentMsmUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.stereotype.Service;

@Service
public class MsmServiceImpl implements MsmService {

    //发送短信
    @Override
    public boolean send(String phone,String code) {
        SendStatus[] statusSet = new SendStatus[0];
        Credential cred = new Credential(TencentMsmUtils.SECRET_ID, TencentMsmUtils.SECRET_KEY);
        //实例化一个客户端配置对象
        ClientProfile clientProfile = new ClientProfile();
        //实例化 SMS 的 client 对象
        SmsClient client = new SmsClient(cred, "",clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        //短信应用ID
        req.setSmsSdkAppid(TencentMsmUtils.APP_ID);
        //设置签名
        req.setSign(TencentMsmUtils.signName);
        //设置模板id
        req.setTemplateID(TencentMsmUtils.TemplateId);
        // 下发手机号码，采用 E.164 标准，+[国家或地区码][手机号]
        String[] phoneNumberSet = {"+86"+phone};
        req.setPhoneNumberSet(phoneNumberSet);
        // 模板参数: 若无模板参数，则设置为空
        System.out.println("手机验证码==============="+code);
        String[] templateParamSet = {code,"5"};
        req.setTemplateParamSet(templateParamSet);
        try {
            /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
             * 返回的 res 是一个 SendSmsResponse 类的实例，与请求对象对应 */
            SendSmsResponse res = client.SendSms(req);
            statusSet = res.getSendStatusSet();
            //System.out.println(SendSmsResponse.toJsonString(res));
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        if (statusSet[0].getCode().equals("Ok")) {
            return true;
        } else {
            return false;
        }
    }
}
